package com.qinglei.recoup.system.manager.adapter.impl;

import com.qinglei.recoup.common.domain.RadarMessage;
import com.qinglei.recoup.common.domain.RadarUserDataDTO;
import com.qinglei.recoup.system.manager.adapter.RadarVersionAdapter;
import com.qinglei.recoup.system.service.BedService;
import com.qinglei.recoup.system.service.RadarService;
import com.qinglei.recoup.system.service.RadarUserDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: 最爱学英语的cxd
 * @slogan: 一入代码深似海，从此红尘是路人
 * @create: 2022-05-05
 **/
@Slf4j
@Service
public class RadarV3Adapter implements RadarVersionAdapter {

    @Resource
    private RadarUserDataService radarUserDataService;

    @Resource
    private RadarService radarService;
    @Resource
    private BedService bedService;

    @Value("${Health.onOrOffV3}")
    private boolean onOrOffV3;


    @Override
    public void handleDRadarData(RadarMessage message) {

        if (message.getType() == RadarMessage.TYPE_USER_DATA) {
            RadarUserDataDTO data = (RadarUserDataDTO) message.getData();
            // 去除翻身状态
            if (data.getS2() > 3) {
                return;
            }

            // 保存雷达上传的用户数据
            radarUserDataService.saveRadarUserData(message);
            // 更新雷达在线状态
            radarService.updateOnlineStatus(message.getSn());
            //更新呼吸过速、呼吸暂停状态
            if(onOrOffV3){
                //breathHeartAlarmV3.updateBreathStatus(message);
            }

        } else if (message.getType() == RadarMessage.TYPE_FALL_DOWN_DATA) {
            // 更新雷达在线状态
            radarService.updateOnlineStatusDownData(message.getSn());

        }

        // 处理雷达关联的床位状态
        try {
            bedService.procRadarData(message, false);
        } catch (MQBrokerException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
    }
}
